package org.rcy.ruicingmarket.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.rcy.framework.api.entity.PageResult;
import org.rcy.framework.api.entity.ResultMessage;
import org.rcy.ruicingmarket.goods.GoodsClient;
import org.rcy.ruicingmarket.goods.GoodsPageQueryRequest;
import org.rcy.ruicingmarket.goods.GoodsVo;
import org.rcy.ruicingmarket.exception.GoodsException;
import org.rcy.ruicingmarket.goods.GoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 吴康桥
 * @Description
 * @date 2022/12/19 14:44
 */

@RestController
@RequestMapping("/goods")
@Tag(name = "goods", description = "商品控制器")
public class GoodsController implements GoodsClient {

	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private GoodsService goodsService;
	@Override
	@Operation(summary = "分页查询", description = "分页查询商品信息", tags = {"goods"})
	@Parameter(name = "Gateway-Token", required = true , in = ParameterIn.HEADER)
	@PostMapping("/pagequery")
	public ResultMessage<PageResult<GoodsVo>> pageQuery(@RequestBody GoodsPageQueryRequest request) {
		try {
			PageResult<GoodsVo> goodsVoPageResult = goodsService.pageQuery(request);
			return ResultMessage.success(goodsVoPageResult);
		} catch (GoodsException e) {
			logger.error(e.getMessage());
		} catch (Exception e){
			logger.error(e.getMessage(),e);
		}
		return ResultMessage.error();
	}

	@Override
	@Operation(summary = "查询单个商品", description = "根据ID查询商品信息", tags = {"goods"})
	@Parameter(name = "Gateway-Token", required = true , in = ParameterIn.HEADER)
	@GetMapping("/{id}")
	public ResultMessage<GoodsVo> getGoodsById(@Parameter(name = "id", required = true , description = "商品ID") @PathVariable("id")Long id) {
		try {
			GoodsVo goodsVo = goodsService.findById(id);
			return ResultMessage.success(goodsVo);
		} catch (GoodsException e) {
			logger.error(e.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return ResultMessage.error();
	}

	@Override
	@Operation(summary = "根据商品名查询", description = "根据商品名查询商品信息", tags = {"goods"})
	@Parameter(name = "Gateway-Token", required = true , in = ParameterIn.HEADER)
	@GetMapping("/byname")
	public ResultMessage getGoodsByName(@Parameter(name = "name", required = true , description = "商品名称") @RequestParam String name) {
		try {
			GoodsVo goods = goodsService.getGoodsByName(name);
			return ResultMessage.success(goods);
		} catch (GoodsException e){
			logger.error(e.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return ResultMessage.error();
	}

	@Override
	@Operation(summary = "删除商品", description = "根据ID删除商品", tags = {"goods"})
	@Parameter(name = "Gateway-Token", required = true , in = ParameterIn.HEADER)
	@DeleteMapping("/{id}")
	public ResultMessage removeById(@Parameter(name = "id", required = true , description = "商品ID") @PathVariable("id") Long id) {
		try {
			goodsService.deleteById(id);
			return ResultMessage.success();
		} catch (GoodsException e){
			logger.error(e.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return ResultMessage.error();
	}

	@Override
	@Operation(summary = "添加商品", description = "添加商品", tags = {"goods"})
	@Parameter(name = "Gateway-Token", required = true , in = ParameterIn.HEADER)
	@PostMapping("/add")
	public ResultMessage addGoods(@RequestBody GoodsVo vo) {
		try {
			Long id = goodsService.addGoods(vo);
			return ResultMessage.success(id);
		} catch (GoodsException e) {
			logger.error(e.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return ResultMessage.error();
	}

}
